<?php
	namespace Admin\Model;
	use \Think\Model;

	class GoodsModel extends Model{

		/**
		 * [getAllspuByCid 取得特定分类下所有spu的信息]
		 * @param  [type] $cid [要传入的分类id]
		 * @return [type]      [数组形式的数据]
		 */
		public function getAllspuByCid($cid,$limit='',$count){
			if(empty($cid)){
				$where = '';
			}else{
				$where = 'gd.catId = '.$cid;
			}

			if(!$count){
				return  $this -> alias('gd')
						  -> field('bd.brandName,bd.logo,pk.skuId pskuid, gd.addtime,gimg.imgUrl,gd.name,gd.id spuid,gdd.status status,gdd.price price,sum(st.store) totalstore')
						  -> join('__GOODS_BRAND__ gbd ON gbd.goodsId=gd.id')
						  -> join('__BRAND__ bd ON bd.id = gbd.brandId')
						  -> join('__GOODSIMG__ gimg ON gimg.spuId=gd.id AND gimg.isMain=1')
						  -> join('__SPU_SKU__ pk ON pk.spuId = gd.id')
						  -> join('__GOODSDETAIL__ gdd ON gdd.skuId = pk.skuId')
						  -> join('__STORE__ st ON st.skuid = gdd.skuid')
						  -> group('pk.spuid')
						  -> limit($limit)
						  -> where($where)
						  -> select();
			}else{
				$result =  $this -> alias('gd')
						  -> field('count(*)')
						  -> join('__GOODSIMG__ gimg ON gimg.spuId=gd.id AND gimg.isMain=1')
						  -> join('__SPU_SKU__ pk ON pk.spuId = gd.id')
						  -> join('__GOODSDETAIL__ gdd ON gdd.skuId = pk.skuId')
						  -> join('__STORE__ st ON st.skuid = gdd.skuid')
						  -> group('pk.spuid')
						  -> limit($limit)
						  -> where($where)
						  -> select();
				return count($result);
			}
			
		}

		/**
		 * [getskuBySpuid 根据传进来的spuid查出该spuid下的所有skuId产品信息]
		 * @param  [type] $spuid [spuid]
		 * @return [type]        [description]
		 */
		public function getskuBySpuid($spuid){

			//查出所有的sku记录
			$skuArr = M('spu_sku')->field('skuId')-> where(['spuId'=>$spuid]) -> select();
			$goodsimg = M('spu_sku')->alias('pk')->join('__GOODSIMG__ gimg ON gimg.spuId=pk.spuId')->where(['gimg.spuId'=>$spuid,'isMain'=>1])->find();

			$arr = [];

			foreach($skuArr as $key => $sku){
	
				//分别查出该sku下的所有信息
				$Arr= M('goodsdetail') -> alias('gdd')
											 -> field('gdd.goodsName gname,gdd.skuId skuid,pro.name proname,val.name valname,gdd.price,gdd.status,st.store')
											 -> join('__GOODS_SKU__ gsk ON gsk.skuId = gdd.skuId')
											 -> join('__PROPERTY__ pro ON pro.id = gsk.proId')
											 -> join('__PROVAL__ val ON val.id=gsk.proValId')
											 -> join('__STORE__ st ON st.skuId = gsk.skuId')
											 -> where(['gdd.skuId'=>$sku['skuid']])
											 -> order('gdd.skuid asc')
											 -> select();
				// return $Arr;
			    //处理sku下的所有信息
			    $rules = '';
			  
				foreach($Arr as $k => $skudetail){
					$rules.= $skudetail['proname'].':'.$skudetail['valname'].' ';
					$price = $skudetail['price'];
					$status = $skudetail['status'];
					$store = $skudetail['store'];
					$skuid = $skudetail['skuid'];
					$gname = $skudetail['gname'];
				}

				$arr[$key] = ['rules'=>$rules,'price'=>$price,'status'=>$status,'store'=>$store,'skuid'=>$skuid,'img'=>$goodsimg['imgurl'],'gname'=>$gname];
			}
			return $arr;
		}


		public function addGoods($data){

	   		$arr = [];

	   		//向goods表中添加数据
	   		M('goods_sku')->startTrans();
	   		M('spu_sku')->startTrans();
	   		M('goodsdetail')->startTrans();
	   		M('store')->startTrans();
	   		M('goodsimg')->startTrans();

	   			$spuId = $data['spuid'];
		   	 	$skuId = M('goods_sku') -> Max('skuId')+1; 
		   	 	foreach($data['val'] as $key => $val){
		   	 		$arr[]= ['skuId'=>$skuId,'proId'=>$data['property'][$key],'proValId'=>$val];
		   	 	}

	   	 	//向goods_sku表中添加信息
	   	 	if(!M('goods_sku') -> addAll($arr)){
	   	 		M('goods_sku')->rollback();
	   	 		return ['mess'=>M('goods_sku')->getError(),'flag'=>false];
	   	 	}else{

	   	 		//向spu_sku关系表中添加数据
	   	 		if(!M('spu_sku') -> add(['spuId'=>$spuId,'skuId'=>$skuId])){
	   	 			M('spu_sku')->rollback();
	   	 			M('goods_sku')->rollback();
	   	 			return ['mess'=>M('spu_sku')->getError(),'flag'=>false];
	   	 		}else{

	   	 			//向goodsdetail表中添加数据
	   	 			if(!M('goodsdetail')-> add(['goodsName'=>$data['goodsname'],'skuId'=>$skuId ,'price'=>$data['price'],'status'=>$data['status'],'isHot'=>0,'SaleNumber'=>0])){
	   	 				M('goodsdetail')->rollback();
	   	 				M('spu_sku')->rollback();
		   	 			M('goods_sku')->rollback();
		   	 			return ['mess'=>M('goodsdetail')->getError(),'flag'=>false];
	   	 			}else{

	   	 				//向库存表中添加数据
   	 					if(!M('store') -> add(['skuId'=>$skuId,'store'=>$data['store']])){
   	 						M('store') ->rollback();
	   	 					M('goodsdetail')->rollback();
		   	 				M('spu_sku')->rollback();
			   	 			M('goods_sku')->rollback();
			   	 			return ['mess'=>M('store')->getError(),'flag'=>false];
   	 					}else{			
	 						M('store') ->commit();
	   	 					M('goodsdetail')->commit();
		   	 				M('spu_sku')->commit();
							M('goods')->commit();
			   	 			M('goods_sku')->commit();
	 						return ['mess'=>M('store')->getError(),'flag'=>true,'spuid'=>$spuId];
   	 					}
	   	 			}
	   	 		}
			}   	 	   	 	
	   }				
	}